﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.JShop.Web.Controls;
using CNVP.Framework;
using CNVP.Framework.Helper;
using System.Data;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin.CommonWindows
{
    public partial class HotLinkProduct_Select_Window : System.Web.UI.Page
    {
        BLL.CNVP_Products bll = new BLL.CNVP_Products();
        public int rplistCount = 0;
        int PageIndex = 1;
        public string SearchType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                ViewState["SearchType"] = "0";
                ProductClassIDBind();
                ddlProductTypeIDBind();
                StartLoad(PageIndex,"");
                #region 验证
                ddlProductTypeID.Attributes.Add("onchange", "ProTypeRefBranks();");
                btUp.Attributes.Add("onclick", "if(!ifCheck()){alert('请选择热链商品！');return false;};");
                #endregion
            }
        }
        //商品分类
        private void ProductClassIDBind()
        {
            new BLL.BLLColumn().BuildAllDDList(this.ddlProductClassID,3);
        }
        //商品类型
        private void ddlProductTypeIDBind()
        {
            ddlProductTypeID.DataSource = new BLL.CNVP_ProductType().LoadAllUseDropDownList();
            ddlProductTypeID.DataTextField = "ProTypeName";
            ddlProductTypeID.DataValueField = "ID";
            ddlProductTypeID.DataBind();
        }
        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex,string orderString)
        {
            int i, j;
            DataTable dt = null;
            SQLConditionInfo[] st = new SQLConditionInfo[12];
            st[0] = new SQLConditionInfo("@IsDelete", "0");
            st[1] = new SQLConditionInfo("@key", Public.FilterSql(txtkey.Text.Trim()));
            st[2] = new SQLConditionInfo("@ProductClassID", ddlProductClassID.SelectedValue);
            st[3] = new SQLConditionInfo("@ProductTypeID", ddlProductTypeID.SelectedValue);
            st[4] = new SQLConditionInfo("@key1", Public.FilterSql(txtkey1.Text.Trim()));
            st[6] = new SQLConditionInfo("@BrandID", Request.Form["listBranks"]);
            st[10] = new SQLConditionInfo("@ProductState", ddlProductState.SelectedValue);
            if (SearchType == "1")
            {
                string ProductClassID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["ProductClassID"]))
                {
                    ViewState["SearchType"] = "2";
                    ProductClassID_Str = Request.QueryString["ProductClassID"];
                    st[7] = new SQLConditionInfo("@ProductClassID", ProductClassID_Str);
                }
                string ProductTypeID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["ProductTypeID"]))
                {
                    ViewState["SearchType"] = "3";
                    ProductTypeID_Str = Request.QueryString["ProductTypeID"];
                    st[8] = new SQLConditionInfo("@ProductTypeID", ProductTypeID_Str);
                }
                string BrandID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["BrandID"]))
                {
                    ViewState["SearchType"] = "4";
                    BrandID_Str = Request.QueryString["BrandID"];
                    st[9] = new SQLConditionInfo("@BrandID", BrandID_Str);
                }
            }
            if (ViewState["SearchType"].ToString() == "5")
            {
                st[11] = new SQLConditionInfo("@ProductState", ViewState["ProductState"]);
            }
            st[5] = new SQLConditionInfo("@SearchType", ViewState["SearchType"]);

            dt = bll.GetPage(this.GetType().Name, PageIndex, 6, orderString, out i, out j, st);
            
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {   
            if (e.CommandName == "Select")
            {
                string ids = e.CommandArgument.ToString();
                BasePage.CloseWindowReturnValues(ids);
            }
            if (e.Item.ItemType == ListItemType.Header)
            {
                LinkButton lkbtnSort = (LinkButton)e.Item.FindControl(e.CommandName.Trim());
                if (ViewState[e.CommandName.Trim()] == null)
                {
                    ViewState[e.CommandName.Trim()] = "ASC";
                }
                if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC")
                {
                    ViewState[e.CommandName.Trim()] = "DESC";
                }
                else
                {
                    ViewState[e.CommandName.Trim()] = "ASC";
                }
                StartLoad(PageIndex," " + e.CommandName.Trim() + " " + ViewState[e.CommandName.Trim()].ToString().Trim());
            }
        }
        string strCode = "";
        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.rpList.Items.Count > 0)
                {
                    for (int i = 0; i < this.rpList.Items.Count; i++)
                    {
                        CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                        if (cbCheck.Checked == true)
                        {
                            strCode += cbCheck.ToolTip + ",";
                        }
                    }
                    strCode = strCode.Substring(0, strCode.Length - 1);
                    BasePage.CloseWindowReturnValues(strCode);
                }
            }
            catch
            { BasePage.CloseWindowReturnValues(strCode); }
        }
        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            SearchType = "2";
            ViewState["SearchType"] = "0";
            StartLoad(PageIndex, "");
        }
        protected void btHighSearch_Click(object sender, EventArgs e)
        {
            SearchType = "2";
            ViewState["SearchType"] = "1";
            StartLoad(PageIndex, "");
        }
    }
}